//
//  AddLadingBillTwoVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/7/3.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "AddLadingBillTwoVC.h"
#import "AddLadingBillStatusView.h"
#import "LadingGoodCell.h"
#import "AddLadingBillThreeVC.h"

@interface AddLadingBillTwoVC ()<UITableViewDelegate,UITableViewDataSource> {
    int page;
}

@property (nonatomic, strong) AddLadingBillStatusView *statusView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *bottomView;

@end

@implementation AddLadingBillTwoVC

-(AddLadingBillStatusView *)statusView {
    if (!_statusView) {
        _statusView = [[AddLadingBillStatusView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 75)];
        _statusView.status = 2;
    }
    return _statusView;
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        
        UIButton *lastBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, (_bottomView.nim_width - 60 - 15) * 0.5, 44)];
        [lastBtn setBackgroundColor:HexColor(0xffffff)];
        lastBtn.layer.masksToBounds = YES;
        lastBtn.layer.cornerRadius = 6;
        lastBtn.layer.borderWidth = 1;
        lastBtn.layer.borderColor = HexColor(0xF2F2F5).CGColor;
        [lastBtn setTitle:@"上一步" forState:0];
        [lastBtn setTitleColor:HexColor(0x585974) forState:0];
        lastBtn.titleLabel.font = BoldFontOfSize(16);
        [lastBtn addTarget:self action:@selector(lastClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:lastBtn];
        
        UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(lastBtn.nim_right + 15, 10, (_bottomView.nim_width - 60 - 15) * 0.5, 44)];
        [nextBtn setBackgroundColor:Color_ThemeColor];
        nextBtn.layer.masksToBounds = YES;
        nextBtn.layer.cornerRadius = 6;
        [nextBtn setTitle:@"下一步" forState:0];
        [nextBtn setTitleColor:HexColor(0xffffff) forState:0];
        nextBtn.titleLabel.font = BoldFontOfSize(16);
        [nextBtn addTarget:self action:@selector(nextClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:nextBtn];
    }
    return _bottomView;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(15, self.statusView.nim_bottom, ScreenW - 30, ScreenH - self.bottomView.nim_height - NaviHeight - self.statusView.nim_height) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        __weak_self;
//        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//            __strong_self;
//            strongSelf->page = 1;
//            [strongSelf requestForList];
//        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page++;
            [strongSelf requestForList];
        }];
    }
    return _tableView;
}

- (void)requestForList {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@(page) forKey:@"page"];
    [dic setObject:self.billModel.tiAddress.w_id forKey:@"id"];
    if (self.dp_id.length) {
        [dic setObject:self.dp_id forKey:@"dp_id"];
    }
    [HttpRequest PostWithApi:ReceiptDeliveryAddLadingGoodList params:dic progress:^(NSProgress * _Nonnull progress) {

    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            if (self->page == 1) {
                [self.billModel.goodArray removeAllObjects];
                self.billModel.goodArray = [LadingGoodMainModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
            }else {
                NSArray *array = [LadingGoodMainModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
                [self.billModel.goodArray addObjectsFromArray:array];
            }
            BOOL has_more = [responseObject[@"has_more"] boolValue];
            if (has_more) {
                self.tableView.mj_footer.hidden = NO;
            }else {
                self.tableView.mj_footer.hidden = YES;
            }
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"新建提货单";
    page = 1;
    [self.view addSubview:self.statusView];
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.tableView];
    [self requestForList];
}

- (void)lastClick {
    [self.rt_navigationController popViewControllerAnimated:YES];
}

- (void)nextClick {
    __block BOOL canGo = NO;
    [self.billModel.goodArray enumerateObjectsUsingBlock:^(LadingGoodMainModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.type == 1) {
            canGo = YES;
            if (obj.default_num <= 0) {
                [MBProgressHUD leShowFailedHUD:self.view message:@"货物的数量需大于0"];
                return;
            }
            if (obj.default_pnum <= 0) {
                [MBProgressHUD leShowFailedHUD:self.view message:@"货物的件数需大于0"];
                return;
            }
        }
    }];
    if (canGo) {
        AddLadingBillThreeVC *vc = [AddLadingBillThreeVC new];
        vc.billModel = self.billModel;
        if (self.dp_id.length) {
            vc.dp_id = self.dp_id;
        }
        [self.rt_navigationController pushViewController:vc animated:YES];
    }else {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请勾选要运输的货物"];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.billModel.goodArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LadingGoodCell *cell = [LadingGoodCell LadingGoodCellWithTableView:tableView];
    LadingGoodMainModel *model = self.billModel.goodArray[indexPath.row];
    cell.model = model;
    cell.selectBlock = ^{
        model.type = model.type == 1 ? 2 : 1;
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    };
    cell.openBlock = ^{
        model.isOpen = !model.isOpen;
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    };
    cell.countBlock = ^(NSString * _Nonnull str) {
        model.default_num = [str intValue];
        model.default_pnum = model.default_num % model.takeNum > 0 ? (model.default_num / model.takeNum) + 1 : model.default_num / model.takeNum;
        [tableView reloadData];
    };
    cell.pieceBlock = ^(NSString * _Nonnull str) {
        model.default_pnum = [str intValue];
        model.default_num = model.default_pnum * model.takeNum;
        [tableView reloadData];
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    LadingGoodCell *cell = [LadingGoodCell LadingGoodCellWithTableView:tableView];
    LadingGoodMainModel *model = self.billModel.goodArray[indexPath.row];
    cell.model = model;
    return cell.height;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.nim_width, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.nim_width, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
